package com.kzd.btgame.main

import android.Manifest
import android.graphics.BitmapFactory
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.*
import com.kzd.btgame.R
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.FragmentMainServiceBinding
import com.kzd.btgame.main.popup.ServiceCodePopup
import com.kzd.btgame.utils.saveImageToGallery
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.image.loadRoundImage
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class MainServiceFragment : OwnerViewBindingFragment<FragmentMainServiceBinding>() {

    private val mServiceCodePopup: ServiceCodePopup by lazy {
        ServiceCodePopup(context = requireContext()) { _, type ->
            requestDangerousPermissions(arrayOf(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
            ), requestCode = type)
        }
    }

    companion object {
        fun newInstance(): MainServiceFragment {
            return MainServiceFragment()
        }
    }

    private val mViewModel: BaseViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentMainServiceBinding
        get() = FragmentMainServiceBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flMainServiceBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {
        //企业微信
        mViewBinding.includeMainService1.llMainServiceWX.setOnClickAntiShakeListener {
            val image = mViewModel.userServiceLiveData.value
                ?.data?.data?.qy_wx_img
            mServiceCodePopup.setContent(if (image.isNullOrEmpty()) {
                R.mipmap.ic_40008
            } else image, 1)
                .showPopupWindow()
        }

        //微信公众号
        mViewBinding.includeMainService1.ivMainServiceWX.setOnClickListener {
            val image = mViewModel.userServiceLiveData.value
                ?.data?.data?.gzh_img
            mServiceCodePopup.setContent(if (image.isNullOrEmpty()) {
                R.mipmap.ic_40007
            } else image, 2)
                .showPopupWindow()
        }

        //客服中心-活动图
        mViewBinding.includeMainService1.ivMainServiceImage.apply {
            loadRoundImage(R.mipmap.ic_40009, radius = ConvertUtils.dp2px((5f)))
        }.setOnClickAntiShakeListener {
            val title = mViewModel.userServiceLiveData.value
                ?.data?.data?.name
            val url = mViewModel.userServiceLiveData.value
                ?.data?.data?.url
            val content = mViewModel.userServiceLiveData.value
                ?.data?.data?.content
            if (!url.isNullOrEmpty()) {
                toMineRichTextActivityUrl(title, url)
            } else if (!content.isNullOrEmpty()) {
                toMineRichTextActivityCon(title, content)
            }
        }

        mViewBinding.includeMainService2.cbMainServicePlatformProblem.setOnCheckedChangeListener { _, isChecked ->
            mViewBinding.includeMainService2.tvMainServicePlatformContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        mViewBinding.includeMainService2.cbMainServiceGameProblem.setOnCheckedChangeListener { _, isChecked ->
            mViewBinding.includeMainService2.tvMainServiceGameContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        mViewBinding.includeMainService2.cbMainServiceTransactionProblem.setOnCheckedChangeListener { _, isChecked ->
            mViewBinding.includeMainService2.tvMainServiceTransactionContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        mViewBinding.includeMainService2.cbMainServiceCurrencyProblem.setOnCheckedChangeListener { _, isChecked ->
            mViewBinding.includeMainService2.tvMainServiceCurrencyContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        mViewBinding.includeMainService2.cbMainServiceMemberProblem.setOnCheckedChangeListener { _, isChecked ->
            mViewBinding.includeMainService2.tvMainServiceMemberContent.visibility =
                if (isChecked) View.VISIBLE else View.GONE
        }

        mViewBinding.includeMainService2.tvMainServicePlatformContent.text = StringBuilder()
            .append(getString(R.string.problem_details_platform_1_title)).append("\n")
            .append(getString(R.string.problem_details_platform_2_title)).append("\n")
            .append(getString(R.string.problem_details_platform_3_title)).append("\n")
            .append(getString(R.string.problem_details_platform_4_title))
        mViewBinding.includeMainService2.tvMainServicePlatformContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.PLATFORM)
        }

        mViewBinding.includeMainService2.tvMainServiceGameContent.text = StringBuilder()
            .append(getString(R.string.problem_details_game_1_title)).append("\n")
            .append(getString(R.string.problem_details_game_2_title)).append("\n")
            .append(getString(R.string.problem_details_game_3_title)).append("\n")
            .append(getString(R.string.problem_details_game_4_title)).append("\n")
            .append(getString(R.string.problem_details_game_5_title)).append("\n")
            .append(getString(R.string.problem_details_game_6_title))
        mViewBinding.includeMainService2.tvMainServiceGameContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.GAME)
        }

        mViewBinding.includeMainService2.tvMainServiceTransactionContent.text = StringBuilder()
            .append(getString(R.string.problem_details_transaction_1_title)).append("\n")
            .append(getString(R.string.problem_details_transaction_2_title)).append("\n")
            .append(getString(R.string.problem_details_transaction_3_title)).append("\n")
            .append(getString(R.string.problem_details_transaction_4_title))
        mViewBinding.includeMainService2.tvMainServiceTransactionContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.TRANSACTION)
        }

        mViewBinding.includeMainService2.tvMainServiceCurrencyContent.text = StringBuilder()
            .append(getString(R.string.problem_details_currency_1_title)).append("\n")
            .append(getString(R.string.problem_details_currency_2_title)).append("\n")
            .append(getString(R.string.problem_details_currency_3_title)).append("\n")
            .append(getString(R.string.problem_details_currency_4_title)).append("\n")
            .append(getString(R.string.problem_details_currency_5_title)).append("\n")
            .append(getString(R.string.problem_details_currency_6_title)).append("\n")
            .append(getString(R.string.problem_details_currency_7_title)).append("\n")
            .append(getString(R.string.problem_details_currency_8_title)).append("\n")
            .append(getString(R.string.problem_details_currency_9_title)).append("\n")
            .append(getString(R.string.problem_details_currency_10_title)).append("\n")
            .append(getString(R.string.problem_details_currency_11_title)).append("\n")
            .append(getString(R.string.problem_details_currency_12_title)).append("\n")
            .append(getString(R.string.problem_details_currency_13_title))
        mViewBinding.includeMainService2.tvMainServiceCurrencyContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.CURRENCY)
        }

        mViewBinding.includeMainService2.tvMainServiceMemberContent.text = StringBuilder()
            .append(getString(R.string.problem_details_member_1_title)).append("\n")
            .append(getString(R.string.problem_details_member_2_title)).append("\n")
            .append(getString(R.string.problem_details_member_3_title)).append("\n")
            .append(getString(R.string.problem_details_member_4_title)).append("\n")
            .append(getString(R.string.problem_details_member_5_title))
        mViewBinding.includeMainService2.tvMainServiceMemberContent.setOnClickAntiShakeListener {
            toProblemDetailsActivity(ProblemDetailsAction.MEMBER)
        }
    }

    override fun initData() {
        mViewModel.userService()
    }

    override fun initViewMode() {
        mViewModel.userServiceLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            mViewBinding.includeMainService1.ivMainServiceImage
                .loadRoundImage(src = data.image ?: R.mipmap.ic_40009,
                    radius = ConvertUtils.dp2px((5f)))
        })
    }

    override fun handlePermissionResult(requestCode: Int, granted: Boolean): Boolean {
        if (!granted) {
            toast("请到应用设置中开启存储权限！")
            return false
        }
        val bitmap = BitmapFactory.decodeResource(resources,
            if (requestCode == 1) R.mipmap.ic_40008 else R.mipmap.ic_40007)
        val fileName = "${if (requestCode == 1) R.mipmap.ic_40008 else R.mipmap.ic_40007}.png"
        if (saveImageToGallery(requireContext(), bitmap, fileName)) {
            toast("已保存")
        } else {
            toast("保存失败")
        }
        return super.handlePermissionResult(requestCode, granted)
    }
}